package com.cheng.example.singleton;

public class SingletonDemo {

    private static SingletonDemo singletonDemo=null;

    private SingletonDemo() {
        System.out.println(Thread.currentThread().getName() + "\t我是构造方法SingletonDemo()");
    }

    public static SingletonDemo getInstence() {
        if (singletonDemo == null) {
            synchronized (SingletonDemo.class) {
                if (singletonDemo == null) {
                    singletonDemo = new SingletonDemo();
                }
            }
        }
        return singletonDemo;
    }
    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            System.out.println(getInstence()==getInstence());
//            System.out.println(getInstence()==getInstence());
//            System.out.println(getInstence()==getInstence());
//        }

        for (int i = 0; i < 10; i++) {
            new Thread(() ->{
                getInstence();
            },String.valueOf(i)).start();
        }
    }
}
